package signal

// m 信号通道映射表
// 格式：map[codeId]map[signalName]chan struct{}
// 例如：m["sh000001"] = map[string]chan struct{}{"cci": <-chan, "macd": <-chan}
var m = make(map[string]map[string]map[string]chan struct{})

func Register(groupName, codeId, signalName string, channel chan struct{}) {
	if _, ok := m[groupName]; !ok {
		m[groupName] = make(map[string]map[string]chan struct{})
	}

	if _, ok := m[groupName][codeId]; !ok {
		m[groupName][codeId] = make(map[string]chan struct{})
	}

	m[groupName][codeId][signalName] = channel
}

// Broadcast 广播信号
func Broadcast(groupName string, codeIds []string) {
	go func() {
		if len(codeIds) == 0 {
			return
		}

		if m1, ok := m[groupName]; !ok {
			for _, codeId := range codeIds {
				if tmp, ok := m1[codeId]; ok {
					for signalName := range tmp {
						tmp[signalName] <- struct{}{}
					}
				}
			}
		}
	}()
}
